<?php
ArrowController::importModel("project.ArrowProject");
/**
 * Router
 * 
 * @version 1.0
 * @license  GNU GPL
 * @author Artur Kmera <artur.kmera@arrowplatform.org> 
 * @todo Rozwinoc o ciastka i pliki, dodac wykrywanie typu wywołania
 */
class ArrowRouter extends ArrowObject{

	public static $INDEX_FILE = "";//"index.php";
	const DATA_REQUEST_PARAMETER = "arrowDataRequest";
	const CONTROLL_REQUEST_PARAMETER = "arrowControllRequest";
	const CONTROLL_ACTION_REQUEST_PARAMETER = "arrowControllActionRequest";
	const PROJECT_PARAMETER = "project";
	const ACTION_BEAN_PARAMETER = "actionBean";
	const TEMPLATE_PARAMETER = "template";
	const TEMPLATE_METHOD_PARAMETER = "templateMethod";
	const MEDIATOR_PARAMETER = "mediator";

	const TEMPLATE_CALL = 0;
	const ACTION_BEAN_CALL = 1;
	const REMOTE_CALL = 2;
	const CONTROLL_CALL = 3;
	const TEMPLATE_METHOD_CALL = 4;
	const CONTROLL_ACTION_CALL = 5;
	
	/**
	 * Mediator
	 *
	 * @var Mediator
	 */
	private $mediator = false;

	/**
	 * Action Bean Call flag
	 * 
	 * @var boolean
	 */
	private $actionBeanCall = false;
	
	/**
	 * Data request call
	 * 
	 * @var boolean
	 */
	private $dataRequestCall = false;	

	/**
	 * Action Bean
	 * 
	 * @var ArrowBeanDescriptor 
	 */
	private $dataProviderBean;	

	/**
	 * Template to display
	 * 
	 * @var ArrowTemplateDescriptor
	 */
	private $temlate;
	
	/**
	 * Call type
	 * 
	 * @var integer
	 */
	private $callType;


	/**
	 * Router instance
	 * @car Router
	 */
	private static $oInstance = null;

	/**
	 * Singleton
	 *
	 * @return ArrowRouter
	 */
	public static function getDefault(){
		if( self::$oInstance == null ){
			self::$oInstance = new ArrowRouter(ArrowRequestContext::getDefault());
		}
		return self::$oInstance;
	}
	
	private function __construct( ArrowRequestContext $request ){
		$project = ArrowController::getDefault()->getProject();
		
		/**
		 * Remembering mediator
		 */
		$this->mediator = $request->getRequest(self::MEDIATOR_PARAMETER);

		/*
		 * Saves action and determinate is a action bean call
		 */
		$action = $request->getRequest(self::ACTION_BEAN_PARAMETER);
		if( $action !== false  ){
			$this->callType = self::ACTION_BEAN_CALL;
			$this->actionBean = $project->getBeansStructure()->getBeanById( $action );
			ArrowLogger::log("[Router] ActionBean id in input founded: '". $this->actionBean->getId()."'" );
		}
		
		/*
		 * Saves action and determinate is a data request call
		 */
		$action = $request->getRequest(self::DATA_REQUEST_PARAMETER);
		if( $action !== false  ){
			$this->callType = self::REMOTE_CALL;
			$this->dataProviderBean = $project->getBeansStructure()->getBeanById( $action );
			ArrowLogger::log("[Router] Data request bean founded: '". $this->dataProviderBean->toString() );
		}
		
		/*
		 * Saves template from input
		 */
		if( $temlateId = $request->getRequest(self::TEMPLATE_PARAMETER) ){
			$this->temlate = $project->getTemplatesStructure()->getTemplateById($temlateId);
			ArrowLogger::log("[Router] Template found: '". $this->temlate );
		}else{
			$template = $project->getTemplatesStructure()->getDefaultTemplate( );
			$this->temlate = $template;
			ArrowLogger::log("[Router] Template not found. Displaying default template: '". $template."'" );
		}
		
		if( $request->getRequest(self::CONTROLL_REQUEST_PARAMETER) ){
			ArrowLogger::log("[Router] Controll call in template: '". $this->temlate );
			$this->callType = self::CONTROLL_CALL;
		}
		
		if( $request->getRequest(self::CONTROLL_ACTION_REQUEST_PARAMETER) ){
			ArrowLogger::log("[Router] Controll action call in template: '". $this->temlate );
			$this->callType = self::CONTROLL_ACTION_CALL;
		}
		
		
		
		if( $request->getRequest(self::TEMPLATE_METHOD_PARAMETER) ){
			ArrowLogger::log("[Router] Method call in template: '". $this->temlate );
			$this->callType = self::TEMPLATE_METHOD_CALL;
		}

	}
	
	/**
	 * Returns call type
	 *
	 * @return boolean
	 */
	public function getCallType(){
		return $this->callType;
	}
	
	
	/**
	 * Returns data provider bean
	 *
	 * @return ArrowBeanDescriptor
	 */
	public function getDataProviderBean(){
		return $this->dataProviderBean;
		
	}
	
	/**
	 * Returns action bean
	 * 
	 * @return ArrowActionBean
	 */
	public function getActionBean(){
		if(!empty($this->mediator))
			$this->getMediator()->getActionBean();
		return $this->actionBean;
	}
	
	/**
	 * Return requested template
	 * 
	 * @return ArrowTemplateDescriptor
	 */
	public function getTemplate(){
		if(!empty($this->mediator))
			$this->getMediator()->getAfterTemplate();
		return $this->temlate;
	}
	
	/**
	 * Returns mediator or false if no mediator call
	 *
	 * @return IArrowMediator
	 */
	public function getMediator(){
		if( $this->mediator != false ){
			ArrowController :: importModel("mediators.ArrowMediatorsList");
			$list = new ArrowMediatorsList();
			return $list->getMediatorInstance($this->mediator);
		}
		return false;
	}	
}
?>